﻿using System;
using System.IO;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmBackupData : Form
    {
        public FrmBackupData()
        {
            InitializeComponent();
        }

        string backupPath = "";

        private void btnBackupPath_Click(object sender, EventArgs e)
        {
            sdlgBackup.FilterIndex = 0;
            sdlgBackup.FileName = "";
            sdlgBackup.Filter = "Bak Files (*.bak)|*.bak|All Files (*.*)|*.*";
            if (sdlgBackup.ShowDialog() == DialogResult.OK)
            {
                txtBackup.Text = sdlgBackup.FileName.ToString();
                txtBackup.ReadOnly = true;
            }
            backupPath = txtBackup.Text.Trim();
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            try
            {
                if (backupPath == "")
                {
                    MessageBox.Show("请先选择数据库备份路径", "提示");
                    return;
                }
                if (File.Exists(backupPath))
                {
                    File.Delete(backupPath);
                }
                PublicMethodsManager publicMethodsManager = new PublicMethodsManager();
                if (publicMethodsManager.BackupDB(backupPath))
                {
                    MessageBox.Show("数据库备份成功", "提示");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("数据库备份失败", "提示");
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
